import { http } from "@/utils/http";

/** 设备分组模型定义 */
export type DeviceGroupModel = {
  id: number;
  groupKey: string;
  name: string;
  purpose: string;
  deptId: number;
  deptName: string;
  remark: string;
  createdAt: string;
  updatedAt: string;
  parentId: number; // 新增父分组ID字段
};

/** 分页获取设备分组列表 */
export const getDeviceGroupListApi = (data: IReqPagerData) => {
  return http.request<Resp<IRespPagerData<DeviceGroupModel>>>(
    "get",
    "/deviceGroup/page-list",
    { params: data }
  );
};

/** 新增设备分组 */
export const addDeviceGroupApi = (data: Partial<DeviceGroupModel>) => {
  return http.request<Resp<null>>("post", "/deviceGroup", { data });
};

/** 更新设备分组 */
export const updateDeviceGroupApi = (id: number, data: Partial<DeviceGroupModel>) => {
  return http.request<Resp<null>>("put", `/deviceGroup/${id}`, { data });
};

/** 删除设备分组 */
export const deleteDeviceGroupApi = (id: number) => {
  return http.request<Resp<null>>("delete", `/deviceGroup/${id}`);
};

/** 根据ID获取设备分组详情 */
export const getDeviceGroupByIdApi = (id: number) => {
  return http.request<Resp<{
    info: DeviceGroupModel
  }>>('get', '/deviceGroup/getOne', {
    params: { id }
  });
};

/** 获取设备分组选项列表（包含父子关系字段） */
export const getDeviceGroupOptionsApi = (params?: { fieldKey?: string; fieldLabel?: string ; deptId?:number}) => {
  return http.request<Resp<{
    options: Array<{
      id: number;           // 分组ID
      name: string;        // 显示文本
      parentId: number;     // 父级ID（关键字段）
    }>
  }>>('get', '/deviceGroup/getOptions', { params });
};

/** 分配设备到分组 */
export const assignDevicesToGroupApi = (groupId: number, deviceIds: number[]) => {
  return http.request<Resp<null>>("post", `/deviceGroup/${groupId}/assignDevices`, {
    data: { deviceIds }
  });
};
